<template>
    <div class="open_index">
        <div class="container">
            <div class="column column-middle body-middle">
                <div id="nav">
                    <ul class="menu">
                        <li class="hover">
                            <a href="/" title="网站首页">首页</a>
                        </li>
                        <li class="">
                            <a target="_blank" href="/">关于博主</a>
                        </li>
                        <li class=""><a href="/">更多</a></li>
                    </ul>
                </div>

                <div class="back-yellow" v-show="!keyword">
                    <h1 class="center bold" itemprop="name">AI伪原创</h1>
                    <hr />
                </div>
                <div class="header-top" v-show="news.length>0">
                    <img  src="https://picsum.photos/360/200?random=9">
                    <div class="img_title">{{keyword}}</div>
                </div>
                <div class="body-article">
                    <div class="searchl" >
                        <section class="search" id="douyin-search">
                            <div class="middle" id="push">
                                <input
                                    type="text"
                                    v-model="keyword"
                                    value=""
                                    autocomplete="on"
                                    placeholder="自己动手输入关键字"
                                    class="ssinput"
                                    @keydown="jSearch"
                                />
                                <input
                                    type="submit"
                                    value="搜索资讯"
                                    @click="jSearch"
                                    class="sszx"
                                />
                            </div>
                        </section>
                    </div>

                    
                    <div class="loading" v-show="loading">正在加载...</div>
                    <div class="loading" v-show="errmsg">{{errmsg}}</div>
                    <div class="body-content"   v-show="news.length>0">                                           
                        <div class="header_bar"  >
                            <h2 class="header_title">{{keyword}}( {{header_title }} )</h2> 
                            <div class="noa">发布时间：{{ vdatetime }}</div>
                            <img :src="header_img">
                        </div>
                        <h5 class="title2" v-show="keyword" >下面围绕"{{ keyword }}"主题解决网友的困惑</h5>
                        <ul class="detail">
                            <li v-for="(item, key) in news">
                                <h3>{{ item.title }}</h3>
                                <p>{{ item.short_article }}</p>
                            </li>
                        </ul>
                    </div>
                    <div class="returntop pink" id="returntop" @click="goToTop">返回顶部</div>
                </div>

                <div class="related">
                    <h2>其他小伙伴的相似问题({{footer_url.length}})</h2>
                    <div class="container">
                        <em v-for="(v,i) in footer_url">
                            <a :href="noParamUrl+'?s='+v">{{v}}</a></em>
                    </div> 
                </div>
                <div class="footer center">
                    <div class="footer-content" v-html="footer_config"> </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "index.vue",
    props: {},
    components: {},
    data() {
        return {
            "{lang_path}": "open.index",
            news: [],
            search_model: false,
            keyword: "",
            vdatetime: "",
            loading: false,
            footer_config:'',
            articel_title:'',
            errmsg:'',
            header_img:'',
            header_title:'',
            footer_url:[],
            noParamUrl:'',
        };
    },
    mounted() {
        this.noParamUrl = window.location.protocol+"//"+window.location.host+""+window.location.pathname;
        const urlParams = window.location.search;
        const params = new URLSearchParams(urlParams);
        this.keyword = params.get("s");
        if (this.keyword == "aaaa") this.keyword = "打工人";

        this.getConfig();
        if (this.keyword) {
            this.search_model = false;
            this.loadData();
        } else {
            this.search_model = true;
        }
    },
    methods: {
        goToTop() {
           window.scrollTo(0, 0);
        },
        getdateTime() {
            const now = new Date();
            const year = now.getFullYear();
            const month = now.getMonth() + 1;
            const day = now.getDate();
            const hours = now.getHours();
            const minutes = now.getMinutes();
            const seconds = now.getSeconds();

            return  `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },
        getConfig() {
            const url = "/web-api/open/config";
            axios
                .get(url)
                .then((res) => {
                    // console.log({ "config>>>": res.data.pagefoot });
                    this.footer_config = res.data.pagefoot.replace(/\n/g, '<br>');                    
                })
                .catch((error) => {
                    let errors = catchError(error);
                    console.log(errors);
                });
        },
        loadData() {
            const url = "/web-api/open/post/spider";
            this.vdatetime = this.getdateTime();
            this.loading = true;  
            document.title = 'Ai博额[' + this.keyword + ']';         
            axios
                .get(url + "?keyword=" + this.keyword)
                .then((res) => {
                    this.loading = false;                   
                    this.search_model = false;
                    if (res.data.status == "success") {
                        this.articel_title = this.keyword;
        
                        this.news = res.data.data.lists;
                        let footer_url = res.data.data.footer_url;
                        if (footer_url) {
                            this.footer_url = footer_url;
                        }
                      
                        this.header_img = "https://picsum.photos/400/220?random="+ Date.now();
             
                        this.header_title = res.data.data.footer_url[0] 
 
                    } else {
                        this.errmsg = "暂无数据";
                    }
                })
                .catch((error) => {
                    this.loading = false;
                    this.search_model = true;
                    let errors = catchError(error);
                    console.log(errors);
                });
        },
        jSearch() {
            if (this.keyword  == "") return false;        
            this.keyword = this.keyword.trim();   
 
            const noParamUrl = window.location.protocol+"//"+window.location.host+""+window.location.pathname;
            //转到当前url地址
            window.location.href = noParamUrl + "?s="+ this.keyword                              
        },
    },
};
</script>

<style lang="scss" scoped>
body {
    margin: 0;
    padding: 0;
    color: white;
}

.container {
    display: flex;

    justify-content: center;
}

.column {
    padding: 0rem;
    text-align: center;
}

.column-left {
    background-color: lightblue;
}

.column-middle {
    background-color: #0e798a;
    width: 80%;
}

.column-right {
    background-color: lightcoral;
}

@media (max-width: 768px) {
    .column {
        flex-basis: 50%;
    }
}

@media (max-width: 480px) {
    .column {
        flex-basis: 100%;
    }
}

.body-middle {
    position: relative;
}

ul li {
    margin: 0;
    list-style: none;
}
a {
color: black;
text-decoration: none;
 cursor: pointer;
}

#nav {
    line-height: 30px;
    background-color: #881328;
    list-style: none;
    padding: 0;
    margin: 0 auto;
}

#nav .menu {
    display: flex;
    margin: 0;
    padding: 10px;
}

#nav li {
    color: #fff;
    font-weight: bold;
    margin: 0;
    list-style: none;
    font-size: 22px;
    float: left;
}

#nav a {
    display: block;
    padding: 0.5rem;
    text-decoration: none;
    color: #fff;
}

#nav a:hover {
    background-color: #ccc;
    color: #fff;
}

.back-yellow {
    background-color: #d2dffb;
    opacity: 0.85;

    font-size: 14px;
    margin: 2em;
    padding-top: 0.1em;
    text-align: left;
}

.back-yellow h1 {
    margin-left: 1em;

    text-decoration: none;
    display: block;
    padding: 1px 0 0 2px;
    color: #ffd700;
    letter-spacing: 0;
    font-size: 30px;
    text-shadow: 0px 1px 0px #346ac1, 0px 2px 0px #abacd6;
}

.searchl {
    margin: 2em;
}

.middle {
    padding: 5px;
}

.ssinput {
    display: inline-block;
    width: calc(100% - 104px);
    height: 35px;
    outline: 0;
    background: #f5f6f7;
    color: #222226;
    vertical-align: top;
    text-indent: 32px;
    border: 1px solid #e8e8ed;
    border-right: none;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    border-radius: 16px 0 0 16px;
    font-weight: 800;
    font: normal 100% "Microsoft Yahei", Helvetica, Tohoma, Arial, Verdana,
        sans-serif;
}

.sszx {
    display: inline-block;
    width: 88px;
    height: 35px;
    outline: 0;
    border: 0 none;
    border-radius: 0 16px 16px 0;
    font-size: 16px;
    line-height: 32px;
    cursor: pointer;
    transition: all 0.2s ease-in;
    background-color: #fc5531;
    text-align: left;
    color: white;

    font-weight: 800;
}

.returntop {
    margin: 1em;
    padding-right: 2rem;
    text-align: right;
    font-weight: 800;
    font-size: 16px;
    cursor: pointer;
    color: #ffd700;
}

.pink {
    color: #e7e8d0;
    font-size: 1em;
    font-weight: bold;
    text-decoration: none;
}

.footer-content {
    color: white;
    text-align: left;
    padding: 1em 1em;
    background-color: black;
}
.detail {
    text-align: left;
    margin-right: 2em;
}
.detail h3 {
    font-size: 24px;
    line-height: 32px;
    color: #dfec09;
    font-weight: 600;
    margin: auto;
}
.detail p {
    font-size: 18px;
    margin: 5px 0;
    line-height: 34px;
    color: white;
}

.body-content h1 {
    text-decoration: none;
    display: block;
    padding: 1px 0 0 2px;
    color: #ffd700;
    letter-spacing: 0;
    font-size: 30px;
    text-shadow: 0px 1px 0px #346ac1, 0px 2px 0px #abacd6;
}

.noa {
    color: white;
    font-size: 1.2em;
    font-weight: bold;
    text-decoration: none;
    margin-bottom: 1.5em ;
}

.loading {
    color: white;
    font-size: 1.2em;
    font-weight: bold;
    text-decoration: none;
    text-align: left;
    margin-left: 3em;
    color: #ffd700;
    text-shadow: 0px 1px 0px #346ac1, 0px 2px 0px #abacd6;
}

 
.related {
    
    text-align: left;
    margin: 1.5em;
    color: black;
    background: powderblue;
    padding: .5em 1em;
}

.related h2 {
    font-size: 1.2em;
    background: white;
    line-height: 2em;
    color: blue;    
    margin: 0;
}

.related .container {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    align-items: flex-start;
    justify-content: flex-start;
    align-content: flex-start;
}

.related em {
    padding: 3px;
    width: 30%;
    font-style: inherit;
    font-size: 1.1em;
}
         
.header_title {
    text-align: center;
    text-decoration: none;
    display: block;
    padding: 1px 0 0 2px;
    color: #ffd700;
    letter-spacing: 0;
    font-size:24px;
    text-shadow: 0px 1px 0px #346ac1, 0px 2px 0px #abacd6;
}     

.header_img {
    display: flex;
    height: 12em;
    margin-left: 3em;
}

.header-top {
    text-align: left;
    margin-left: 2em;
    position: relative;
    margin-top: 1em;
}

.header-top img {}
.header-top .img_title {
    content: '';
    position: absolute;
    top: 1.5em;
    background-color: rgba(0, 0, 0, 0.5);
    font-size: 2.0em;
    padding: 0.5em 0em;
    border: 1px solid #fff;
    width: 360px;
    text-align: center;
    color: #fff;
}

.title2 {
    text-align: left;
    margin-left: 3em;
    color: #fff;
}


@media (max-width: 768px) {
   .header-top {
       text-align: center;
       margin-left: 0em;
   }

   .header_bar>img {
        max-width: 100%
   }
}
</style>
